import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index";
Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/index",
    },
    {
      path: "/index",
      component: () => import("@/pages/Main.vue"),
      children: [
        {
          path: "",
          component: () => import("@/components/main/index.vue"),
        },
        {
          path: "/list",
          name: "UserList",
          component: () => import("@/components/main/UserList.vue"),
          meta: {
            //设置权限
            title: "会员信息",
            breadcrumb: ["会员管理", "会员信息"], // 面包屑层级,
          },
        },
        {
          path: "/cart",
          name: "UserCart",
          component: () => import("@/components/main/Cart.vue"),
          meta: {
            title: "购物车信息",
            breadcrumb: ["会员管理", "购物车信息"],
          },
        },
        {
          path: "/address",
          name: "UserAddress",
          component: () => import("@/components/main/address.vue"),
          meta: {
            title: "收货地址",
            breadcrumb: ["会员管理", "收货地址"],
          },
        },
        {
          path: "/brand",
          name: "Brand",
          component: () => import("@/components/main/brand.vue"),
          meta: {
            title: "品牌管理",
            breadcrumb: ["商品管理", "品牌管理"],
          },
        },
        {
          path: "/banner",
          name: "Banner",
          component: () => import("@/components/main/banner.vue"),
          meta: {
            title: "轮播图管理",
            breadcrumb: ["商品管理", "轮播图管理"],
          },
        },
        {
          path: "/category",
          name: "Category",
          component: () => import("@/components/main/category.vue"),
          meta: {
            title: "商品分类管理",
            breadcrumb: ["商品管理", "商品分类管理"],
          },
        },
        {
          path: "/attr",
          name: "Attr",
          component: () => import("@/components/main/attr.vue"),
          meta: {
            title: "规格属性管理",
            breadcrumb: ["商品管理", "规格属性管理"],
          },
        },
        {
          path: "/attrValue",
          name: "AttrValue",
          component: () => import("@/components/main/attrValue.vue"),
          meta: {
            title: "规格属性值管理",
            breadcrumb: ["商品管理", "规格属性管理", "规格属性值管理"],
          },
        },
        {
          path: "/goods",
          name: "Goods",
          component: () => import("@/components/main/goods.vue"),
          meta: {
            title: "商品信息管理",
            breadcrumb: ["商品管理", "商品信息管理"],
          },
        },
        {
          path: "/relative",
          name: "relative",
          component: () => import("@/components/main/GoodsRelation.vue"),
          meta: {
            title: "商品关联属性",
            breadcrumb: ["商品管理", "商品信息管理","商品关联属性"],
          },
        },
        {
          path: "/sku",
          name: "sku",
          component: () => import("@/components/main/goodsSku.vue"),
          meta: {
            title: "SKU管理",
            breadcrumb: ["商品管理", "SKU管理"],
          },
        },
        {
          path: "/skuRelative",
          name: "skuRelative",
          component: () => import("@/components/main/goodsSkuRelative.vue"),
          meta: {
            title: "sku关联属性",
            breadcrumb: ["商品管理", "SKU管理","sku关联属性"],
          },
        },
        {
          path: "/orderList",
          name: "orderList",
          component: () => import("@/components/main/orderList.vue"),
          meta: {
            title: "订单信息",
            breadcrumb: ["订单管理", "订单信息"],
          },
        },
        {
          path: "/orderInfo",
          name: "orderInfo",
          component: () => import("@/components/main/orderInfo.vue"),
          meta: {
            title: "订单详细信息",
            breadcrumb: ["订单管理", "订单信息","订单详细信息"],
          },
        },
      ],
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("@/pages/Login.vue"),
      meta: {
        title: "登录",
        breadcrumbHidden: true, // 不在面包屑显示
      },
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    //从store中获取用户登录信息
    let isLogin = store.state.userStore.isLogin;
    //判断用户是否登录
    if (!isLogin) {
      //提示未登录
      Vue.prototype.$message.error("无权访问，请先登录！");
      //未登录精心拦截，跳转到登录页面
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
